package com.zadtek.shopmall.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.bean.BuyingProductHeaderItem;
import com.zadtek.shopmall.bean.GuesLikeProductItem;
import com.zadtek.shopmall.bean.MultiItem;
import com.zadtek.shopmall.bean.ShopCartProductItem;

import java.util.List;

/**
 * Created by Zackv on 2018/4/8.
 */

public class ShopCartAdapter extends BaseMultiItemQuickAdapter<MultiItem,HideItemViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    private Context context;
    public ShopCartAdapter(Context context,List<MultiItem> data) {
        super(data);
        addItemType(MultiItem.Type_Car_Product, R.layout.buy_product_item);
        addItemType(MultiItem.Type_Car_Product_header, R.layout.buy_product_header );
        addItemType(MultiItem.Type_Product, R.layout.product_item);
        addItemType(MultiItem.Type_Home_Header, R.layout.head_item);
        this.context = context;
    }

    @Override
    protected void convert(HideItemViewHolder helper, MultiItem item) {
        switch (helper.getItemViewType()){
            case MultiItem.Type_Car_Product:
                if(item.isDeleted()){
                    helper.setVisibility(false);
                    break;
                }else{
                    helper.setVisibility(true);
                }
                ShopCartProductItem buyingProductItem = (ShopCartProductItem) item.getData();
                helper.setText(R.id.pro_title,buyingProductItem.getGoods_name());
                helper.setText(R.id.pro_price,String.valueOf(buyingProductItem.getGoods_price()));
                helper.setText(R.id.pro_desc,buyingProductItem.getGoods_attr());
                helper.setText(R.id.tv_count,String.valueOf(buyingProductItem.getGoods_number()));
                Glide.with(context).load(buyingProductItem.getGoods_thumb()).placeholder(R.drawable.placeholder).into((ImageView) helper.getView(R.id.pro_img));

                helper.setChecked(R.id.cb_choose,item.isSelected());
                helper.addOnClickListener(R.id.cb_choose);
                helper.addOnClickListener(R.id.btn_add);
                helper.addOnClickListener(R.id.btn_remove);
                helper.addOnClickListener(R.id.btn_delete);
                break;
            case MultiItem.Type_Car_Product_header:
                if(item.isDeleted()){
                    helper.setVisibility(false);
                    break;
                }

                BuyingProductHeaderItem shopName = (BuyingProductHeaderItem) item.getData();
                if(shopName.isSelfSell()){
                    helper.setVisible(R.id.img_detail,false);
                }
                helper.setText(R.id.tv_shop_name,shopName.getHeader());
                helper.setChecked(R.id.cb_choose_all_shop,item.isSelected());
                helper.addOnClickListener(R.id.cb_choose_all_shop);
                break;
            case MultiItem.Type_Product:
                GuesLikeProductItem itemData= (GuesLikeProductItem) item.getData();
                helper.setText(R.id.product_title,itemData.getGoods_name());
                helper.setText(R.id.product_price,itemData.getGoods_price());
                helper.getView(R.id.product_sales).setVisibility(View.GONE);
                Glide.with(context).load(itemData.getGoods_img()).asBitmap().centerCrop().placeholder(R.drawable.placeholder).into((ImageView) helper.getView(R.id.product_img));
                break;
            case MultiItem.Type_Home_Header:
                break;
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if(manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position);
                    switch (type){
                        case MultiItem.Type_Product:
                            return 1;
                        default:
                            return 2;
                    }
                }
            });
        }
    }
}
